package kr.co.valueworks.fishing.activity;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import kr.co.valueworks.fishing.R;
import kr.co.valueworks.fishing.R.id;
import kr.co.valueworks.fishing.R.layout;
import kr.co.valueworks.fishing.R.string;
import kr.co.valueworks.fishing.model.FishingDBHelper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView.OnEditorActionListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class LocalityActivity extends BaseActivity{

	FishingDBHelper fishingDBHelpler;
	String area;
	
	public String getArea() {
		return area;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.locality_list);
		
		AdView adView = (AdView)findViewById(R.id.adView);
	    AdRequest request = new AdRequest();
		adView.loadAd(request);
		
        Intent intent = getIntent();
        area = intent.getStringExtra("area");
        
		final EditText editText = (EditText)findViewById(R.id.searchEditText);
		editText.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(actionId == EditorInfo.IME_ACTION_SEARCH){ 		
					goSearch();
				}
				return false;
			}
		});		
		ImageButton imgBtn = (ImageButton)findViewById(R.id.searchBtn);
		imgBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goSearch();				
			}
		});
		
        try {
	        fishingDBHelpler = new FishingDBHelper(this);
			Cursor cursor;
			SQLiteDatabase db = fishingDBHelpler.getReadableDatabase();
			
			String sql = String.format("SELECT _id, locality, count(*) cnt FROM fishing " +
					"WHERE area LIKE '%s%%' AND status = 'Y' GROUP BY locality", area);
			Log.d("Fishing", sql);
			cursor = db.rawQuery(sql, null);
			startManagingCursor(cursor);
			
			if(cursor.getCount() > 0)
			{
				LocalityCursorAdapter adapter = new LocalityCursorAdapter(this,cursor);		
				ListView list = (ListView)findViewById(R.id.localityListView);
		        list.setAdapter(adapter);				
			}
			else
			{
				Toast.makeText(this, R.string.noData, Toast.LENGTH_SHORT).show();
				finish();
			}

			/*
			adapter = new SimpleCursorAdapter(this, 
					android.R.layout.simple_list_item_1,
					cursor, new String[] { "locality" }, 
					new int[] { android.R.id.text1 });
			ListView list = (ListView)findViewById(R.id.localityListView);
			list.setAdapter(adapter);

			list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					 	final Cursor cursor = (Cursor) parent.getItemAtPosition(position);
					 	
						String locality = cursor.getString(1);
						Intent intent = new Intent(LocalityActivity.this, Fishing.class);
			    		intent.putExtra("area", area);
			    		intent.putExtra("locality", locality);
						startActivity(intent);					
					
				}
			});	
			*/        
        } catch (Exception e) {
        	e.getStackTrace();
        	Log.d("Fishing", e.getMessage());
        }
	}
	
	protected void goSearch()
	{
		EditText editText = (EditText)findViewById(R.id.searchEditText);
		String keyword = editText.getText().toString().trim();
		if(keyword.equals("")) return;
		Intent intent = new Intent(LocalityActivity.this, Fishing.class);
		intent.putExtra("type", "search");
		intent.putExtra("keyword", keyword);
		intent.putExtra("area", area);
		startActivity(intent);			
	}	
}

class LocalityCursorAdapter extends CursorAdapter {
	String TAG = Fishing.class.getName();
	Context mainContext;
	public LocalityCursorAdapter(Context context, Cursor c) {
		super(context, c);
		mainContext = context;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.locality_item, parent, false);
        return view;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		
		try{
			final String locality = cursor.getString(cursor.getColumnIndex("locality"));
			final String cnt = cursor.getString(cursor.getColumnIndex("cnt"));
			
			//String title = locality + " (" + cnt + ")";
			((TextView)view.findViewById(R.id.localityTextView)).setText(locality + " ("+cnt+")");
			//((TextView)view.findViewById(R.id.countTextView)).setText("("+cnt+")");
			
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mainContext, Fishing.class);
					intent.putExtra("area", ((LocalityActivity)mainContext).getArea());
		    		intent.putExtra("locality", locality);
					mainContext.startActivity(intent);
				}
			});		
		}
		catch(Exception e)
		{
			Log.v(TAG,e.getMessage());
		}
	}	
}